<template>
  <div class="kcgl_box">
    <header-comm></header-comm>
    <div class="header_sele" :style="$parent.$parent.collapsed==false?'left: 10%':'left:80px'">
      <div class="add_qy">
        <a-button type="primary" @click="dckcdata">导出库存</a-button>
        <a-button type="primary" @click="gxnewdate">更新时间</a-button>
        <a-popconfirm title="确定删除吗?" @confirm="start">
          <a-button type="primary">选择删除</a-button>
        </a-popconfirm>
        <a-popconfirm title="确定全部删除吗?" @confirm="alldelstart">
          <a-button type="primary">全部删除</a-button>
        </a-popconfirm>
        <!-- <a-button type="primary" @click="start">选择删除</a-button> -->
        <span>{{$store.state.gysmc}}</span>
      </div>
      <div>
        <span>查询：</span>
        <div class="sele">
          <a-input placeholder="请输入查询关键字 " style="width: 200px" @change="onSearch" />
          <a-icon type="search" class="search" />
        </div>
      </div>
    </div>
    <!-- 表格-================= -->
    <div class="bcg">
      <div class="kcgl">
        <a-table
          @change="handleChangelist"
          :columns="columns"
          :pagination="pagination"
          :dataSource="data"
          bordered
          :loading="loading"
          :rowSelection="rowSelection"
        >
          <template v-for="titletext in []" :slot="titletext" slot-scope="text, record, index">
            <div :key="titletext"></div>
          </template>

          <template slot="operation" slot-scope="text, record, index">
            <div class="editable-row-operations">
              <a-popconfirm title="确定删除吗?" class="del" @confirm="() => kcdelete(record,record.key)">
                <a href="javascript:;">删除</a>
              </a-popconfirm>
              <!-- <span class="del" @click="()=>kcdelete(record,record.key)">删除</span> -->
            </div>
          </template>
        </a-table>
      </div>
    </div>
    <!-- 表格-================= -->
    <div class="fyloading" v-if="fyflag">
      <a-spin size="large" tip="正在翻译...,过程可能需要一段时间，请耐心等待一下...">
        <!-- <div class="spin-content">
        正在翻译...,过程可能需要一段时间，请耐心等待一下
        </div>-->
      </a-spin>
    </div>
  </div>
</template>

<script>
import qs from "qs";
import headerComm from "../header-comm/header.vue";
const columns = [
  {
    title: "型号",
    dataIndex: "XL"
  },
  {
    title: "品牌",
    dataIndex: "SCCJC"
  },
  {
    title: "分类",
    dataIndex: "tcfenleiName"
  },
  {
    title: "数量",
    dataIndex: "WLKGSL"
  },
  {
    title: "价格",
    dataIndex: "WLTEJIA"
  },
  {
    title: "产地",
    dataIndex: "SCCD"
  },
  {
    title: "库房",
    dataIndex: "KFMC"
  },
  {
    title: "内径",
    dataIndex: "nj"
  },
  {
    title: "外径",
    dataIndex: "wj"
  },
  {
    title: "宽度",
    dataIndex: "kd"
  },
  {
    title: "备注",
    dataIndex: "BEIZHU"
  },
  {
    title: "更新时间",
    dataIndex: "datatime"
  },
  {
    title: "操作",
    dataIndex: "operation",
    scopedSlots: { customRender: "operation" }
  }
];
//上传图片
function getBase64(img, callback) {
  const reader = new FileReader();
  let file = [];
  file = reader;
  reader.addEventListener("load", () => callback(reader.result));
  reader.readAsDataURL(img);
  sessionStorage.setItem("file", reader);
}
const data = [];
export default {
  inject: ["reload"],
  data() {
    this.cacheData = data.map(item => ({ ...item }));
    return {
      data,
      columns,
      sele: "",
      serchvalue: "",
      pagination: {
        defaultPageSize: 10,
        showTotal: total => `共 ${total} 条数据`,
        total: 0
      },
      loading: true,
      datakh: [],
      // dataSource: [],
      // datakhId: "",
      khvalue: "",

      selectedRowKeys: [],
      fyflag: false
    };
  },
  components: {
    headerComm
  },
  // if(value=='中文'){
  //   this.cnshow='1'
  // }
  //  if(value=='英文'){
  //   this.enshow='1'
  // }
  //  if(value=='德文'){
  //   this.dwshow='1'
  // }
  //  if(value=='日文'){
  //   this.jashow='1'
  // }
  // if(value=='俄文'){
  //   this.rushow='1'
  // }
  methods: {
    //搜索框
    onSearch(e) {
      this.serchvalue = value;
      var value = e.target.value;
      console.log(e.target.value);
      this.handleChangelist(this.pagination, value);
    },

    //表格
    handleChangelist(pagination, khmc) {
      // const newData = [...this.data]
      // const target = newData.filter(item => key === item.key)[0]
      this.axios
        .post(
          "kucun/AdminBusinessListKc.php",
          qs.stringify({
            sqdwid: this.$store.state.qycpymID,
            filed: khmc,
            page: pagination.current,
            pageSize: pagination.defaultPageSize
          })
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            // console.log(this.$store.state.qycpymID)
            this.data = res.data.result.data;
            this.pagination.total = Number(res.data.result.total);
            this.loading = false;
            this.cacheData = this.data.map(item => ({
              ...item
            }));
            for (var i = 0; i < res.data.result.data.length; i++) {
              this.data[i].key = res.data.result.data[i].ID_NUM;
            }
            sessionStorage.setItem("total", res.data.result.total);
          } else {
            this.data = [];
            this.loading = false;
          }
        });
      // if (target) {
      //   target[column] = value
      // this.data=newData
      // }
    },

    //删除客户列表
    kcdelete(value, key) {
      console.log(value);
      const dataSource = [...this.data];
      this.data = dataSource.filter(item => item.key !== key);
      this.axios
        .post(
          "kucun/AdminBusinessDeleteKc.php",
          qs.stringify({ id_num: value.ID_NUM })
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
          }
        });
    },
    //选择删除
    start() {
      this.loading = true;
      const newData = [...this.data];
      let selectedRowKeys = this.selectedRowKeys;
      console.log(selectedRowKeys);
      this.axios
        .post(
          "kucun/AdminBusinessBatchDeleteKc.php",
          qs.stringify({
            id_num: selectedRowKeys
          })
        )
        .then(res => {
          console.log(res);
          //更新数据成功
          if (res.data.code == 303) {
            this.loading = false;
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
            selectedRowKeys.map(rowkeys => {
              this.data = this.data.filter(item => item.key !== rowkeys);
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },
     //全部删除
    alldelstart() {
      this.axios
        .post(
          "kucun/DeleteBusinessKc.php",
          qs.stringify({
            sqdwid: this.$store.state.qycpymID,
            ly: "导入"
          })
        )
        .then(res => {
          //更新数据成功
          if (res.data.code == 303) {
            this.loading = false;
            this.data = [];
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
          } else {
            this.loading = false;
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },
    //批量导出
    dckcdata() {
      this.axios
        .post(
          "kucun/PushBusinessKc.php",
          qs.stringify({ sqdwid: this.$store.state.qycpymID })
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
            location.href = res.data.result;
          } else {
            this.$notification["error"]({
              message: "更新失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },
    gxnewdate() {
      this.axios
        .post(
          "kucun/RefreshKcTime.php",
          qs.stringify({ sqdwid: this.$store.state.qycpymID })
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "更新成功",
              description: res.data.msg,
              duration: 1.5
            });
            this.reload();
          } else {
            this.$notification["error"]({
              message: "更新失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    }
  },
  computed: {
    rowSelection() {
      const { selectedRowKeys } = this;
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(
            `selectedRowKeys: ${selectedRowKeys}`,
            "selectedRows: ",
            selectedRows
          );
          this.selectedRowKeys = selectedRowKeys;
        }
      };
    }
  },
  created() {
    this.handleChangelist(this.pagination);
  }
};
</script>

<style lang='less' >
.kcgl_box {
  .header_sele {
    position: absolute;
    top: 55px;
    // left: 200px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 999;
    text-align: right;
    width: 90%;
    background-color: #fff;
    box-shadow: 5px 5px 5px #ccc;
    // height: 50px;
    padding: 10px 20px;
    .sele {
      position: relative;
      display: inline-block;
      i {
        position: absolute;
        top: 10px;
        right: 8px;
      }
    }
    .add_qy {
      width: auto;
      text-align: right;
      padding: 10px 0;
      display: inline-block;
      height: 100%;
      button {
        margin: 0 10px;
      }
    }
  }
  .kcgl {
    width: 98%;
    position: absolute;
    top: 152px;
    left: 1%;
    background-color: #fff;
  }

  .fyloading {
    position: absolute;
    width: 90% !important;
    border: 1px solid #91d5ff;
    background: rgba(255, 255, 255, 0.5);
    padding: 30px;
    height: 800px !important;
    z-index: 999999 !important;
    top: 108px;
    left: 201px;
    // opacity: .5!important;
    .ant-spin-blur {
      opacity: 1 !important;
    }
    .ant-spin-text {
      font-size: 18px !important;
    }
    // .spin-content{
    //   color: #000;
    //   font-size: 16px!important;
    //   top: 100px!important;
    // }
  }
  .ant-spin-nested-loading > div > .ant-spin {
    top: 100px !important;
  }
  .del {
    padding-right: 15px;
    color: #40a9ff;
    cursor: pointer;
  }
}
</style>
